package com.example.hangzhou.welbeing.mytestdemoapp.adpater;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.hangzhou.welbeing.mytestdemoapp.R;

import java.util.ArrayList;

import cn.jesse.nativelogger.NLogger;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/4/17
 *
 * @desc 东风山河绿，春已不折柳，繁花看不尽，处处惹人留
 * @desc 佛祖镇楼，bug辟容易
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private ArrayList<String> mData = new ArrayList<>();
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;

    private View mHeaderView;



    public void setHeaderView(View headerView) {
        mHeaderView = headerView;

        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }
    @Override
    public int getItemViewType(int position) {
        if(mHeaderView != null && position == 0) {
            return TYPE_HEADER;
        }else{
            return TYPE_NORMAL;
        }


    }

    public void addDatas(ArrayList<String> datas) {
        mData.addAll(datas);
        notifyDataSetChanged();
    }

    public void updateData(ArrayList<String> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER) return new ViewHolder(mHeaderView);
        // 实例化展示的
         View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_item, parent, false);
        // 实例化
        ViewHolder  viewHolder = new ViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) { //
        NLogger.e("TAG","mHeaderView==========position======"+position);
        if(getItemViewType(position) == TYPE_HEADER) return;

        if(holder instanceof ViewHolder){
            // 绑定数据  position 从1开始的
            holder.mTv.setText(mData.get(position -1));

        }

    }

    @Override
    public int getItemCount() {
        int count = (mData == null ? 0 : mData.size());
        if(mHeaderView != null){
            count = count +1;
        }
        return count;
        //return 0;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager){
            GridLayoutManager gridManager  =    (GridLayoutManager) manager;
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if(getItemViewType(position) == TYPE_HEADER  ){
                        return gridManager.getSpanCount();
                    }else {
                        return 1;
                    }
                   // return getItemViewType(position) == TYPE_HEADER ?gridManager.getSpanCount():1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
       ViewGroup.LayoutParams lp =  holder.itemView.getLayoutParams();
        if(lp != null && lp instanceof StaggeredGridLayoutManager.LayoutParams ){
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan(true);
//          if(holder.getLayoutPosition() == 0 ){
//              p.setFullSpan(true);
//
//          }

        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTv;
        public ViewHolder(View itemView) {
            super(itemView);
            mTv = (TextView) itemView.findViewById(R.id.item_tv);
        }
    }
}

